package com.com.demo2;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class TCPServer2 {
    public static void main(String[] args) throws Exception {
        int port = 8088;
        Scanner scanner = new Scanner(System.in);
        ServerSocket ss = new ServerSocket(port);
// 死循环
        while (true) {
// 接收客户端的请求
            Socket socket = ss.accept();
// 接收客户端的数据
            DataInputStream dis = new DataInputStream(socket.getInputStream());
            String msg = dis.readUTF();
            System.out.println("服务器接收到客户端消息：" + msg);
// 写回给客户端
            DataOutputStream dos = new
                    DataOutputStream(socket.getOutputStream());
            System.out.print("请响应:");
            msg = scanner.next();
            dos.writeUTF(msg);
            dos.close();
            dis.close();
            socket.close();
        }
    }
}
